package com.zcp.todayheadline;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.zcp.todayheadline.adapter.MyBase;
import com.zcp.todayheadline.fragment.CareFragment;
import com.zcp.todayheadline.fragment.LoginFragment;
import com.zcp.todayheadline.fragment.NewsFragment;
import com.zcp.todayheadline.fragment.VideoFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main2)
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {


    @ViewInject(R.id.image_home)
    ImageView image_home;

    @ViewInject(R.id.image_video)
    ImageView image_video;

    @ViewInject(R.id.image_love)
    ImageView image_love;

    @ViewInject(R.id.image_login)
    ImageView image_login;

    @ViewInject(R.id.tv_home)
    TextView tv_home;

    @ViewInject(R.id.tv_video)
    TextView tv_video;

    @ViewInject(R.id.tv_love)
    TextView tv_love;

    @ViewInject(R.id.tv_login)
    TextView tv_login;

    TextView tvs[];
    Fragment fragment;
    NewsFragment newsf;
    VideoFragment video;
    CareFragment care;
    LoginFragment login;
    public static DisplayImageOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        //找控件
        initView();
        setFirst();

        if (newsf == null) {
            newsf = new NewsFragment();
        }

        addFragment(newsf);

    }

    //设置首页按钮默认背景
    public void setFirst() {
        image_home.setImageResource(R.drawable.b_newhome_tabbar_press);
        tv_home.setTextColor(Color.RED);
    }

    //找控件
    public void initView() {
        //文字数组
        tvs = new TextView[]{tv_home, tv_video, tv_love, tv_login};
        //按钮监听
        image_home.setOnClickListener(this);
        image_video.setOnClickListener(this);
        image_love.setOnClickListener(this);
        image_login.setOnClickListener(this);
    }

    //按钮监听
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_home:
                image_home.setImageResource(R.drawable.b_newhome_tabbar_press);
                image_video.setImageResource(R.drawable.b_newvideo_tabbar);
                image_love.setImageResource(R.drawable.b_newcare_tabbar);
                image_login.setImageResource(R.drawable.b_newnologin_tabbar);

                //文字改变
                setTextBg(0);
                if (newsf == null) {
                    newsf = new NewsFragment();
                }

                addFragment(newsf);
                break;
            case R.id.image_video:
                image_home.setImageResource(R.drawable.b_newhome_tabbar);
                image_video.setImageResource(R.drawable.b_newvideo_tabbar_press);
                image_love.setImageResource(R.drawable.b_newcare_tabbar);
                image_login.setImageResource(R.drawable.b_newnologin_tabbar);

                //文字改变
                setTextBg(1);

                if (video == null) {
                    video = new VideoFragment();
                }

                addFragment(video);
                break;
            case R.id.image_love:
                image_home.setImageResource(R.drawable.b_newhome_tabbar);
                image_video.setImageResource(R.drawable.b_newvideo_tabbar);
                image_love.setImageResource(R.drawable.b_newcare_tabbar_press);
                image_login.setImageResource(R.drawable.b_newnologin_tabbar);

                //文字改变
                setTextBg(2);
                if (care == null) {
                    care = new CareFragment();
                }
                addFragment(care);
                break;
            case R.id.image_login:
                image_home.setImageResource(R.drawable.b_newhome_tabbar);
                image_video.setImageResource(R.drawable.b_newvideo_tabbar);
                image_love.setImageResource(R.drawable.b_newcare_tabbar);
                image_login.setImageResource(R.drawable.b_newnologin_tabbar_press);

                //文字改变
                setTextBg(3);

                if (login == null) {
                    login = new LoginFragment();
                }

                addFragment(login);
                break;
        }
    }

    //文字颜色更改
    public void setTextBg(int index) {
        for (int i = 0; i < tvs.length; i++) {
            //判断
            if (i == index) {
                tvs[i].setTextColor(Color.RED);
            } else {
                tvs[i].setTextColor(Color.BLACK);
            }
        }
    }

    //添加Fragment
    public void addFragment(Fragment ff) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        //判断
        /*
        * fragment代表原有的容器
        * ff代表新的fragment,如果在旧的fragment上有内容,就将其隐藏,新的显示出来
        * 如果旧的为空,且新的还没有被添加,就将新的内容添入
        * */
        if (fragment != null) {
            ft.hide(fragment);
        }
        if (!ff.isAdded()) {
            ft.add(R.id.f_content, ff);
        }
        ft.show(ff);
        ft.commit();
        fragment = ff;

    }

    @Override
    public void onBackPressed() {
        if (MyBase.mCurPopupWindow != null && MyBase.mCurPopupWindow.isShowing()) {
            MyBase.mCurPopupWindow.dismiss();
        } else {
            super.onBackPressed();
        }
    }
}
